import request from "@/utils/request";
import { praseStrEmpty } from "@/utils/common";
import aes from "@/utils/cryptoUtil";

// 铁塔视角查询用户列表
export function listUserTT(query) {
  return request({
    url: "/ttadmin/ttview/system/user/tenant/list",
    method: "post",
    data: query,
  });
}

// 铁塔视角导出用户(1.5.50版本)
export function exportUserTT1() {
  return "/ttexport/SysUserExport/exportUser";
}

// 查询用户列表
export function listUser(query) {
  return request({
    url: "/admin/system/user/list",
    method: "get",
    params: query,
  });
}

// 查询用户详细
export function getUser(userId, industyCode, appVerCode) {
  return request({
    url:
      "/admin/system/user/base/detail/" +
      praseStrEmpty(userId) +
      "/" +
      praseStrEmpty(industyCode) +
      "/" +
      praseStrEmpty(appVerCode),
    method: "get",
  });
}

// 新增用户
export function addUser(data) {
  return request({
    url: "/admin/system/user/add",
    method: "post",
    data: data,
  });
}

export function checkUserNameIsExist(userName) {
  return request({
    url: "/admin/system/user/checkUserNameIsExist",
    method: "post",
    params: {
      userName: userName,
    },
  });
}

// 修改用户
export function updateUser(data) {
  return request({
    url: "/admin/system/user/update",
    method: "post",
    data: data,
  });
}

// 删除用户
export function delUser(userId) {
  return request({
    url: "/admin/system/user/del/" + userId,
    method: "get",
  });
}

// 导出用户
export function exportUser() {
  return "/export/exportSysUser";
}

// 导入用户
export function importUser() {
  return "/admin/system/user/importSysUser";
}

// 用户密码重置
export function resetUserPwd(userId) {
  return request({
    url: "/admin/system/user/resetPwd",
    method: "post",
    params: { userId: userId },
  });
}

// 用户解锁
export function unlockUser(userName) {
  return request({
    url: "/admin/system/user/unlock",
    method: "post",
    params: { userName: userName },
  });
}

// 用户状态修改
export function changeUserStatus(userId, status) {
  const data = {
    userId,
    status,
  };
  return request({
    url: "/admin/system/user/changeStatus",
    method: "post",
    data: data,
  });
}

// 查询用户个人信息
export function getUserProfile() {
  return request({
    url: "/admin/base/system/user/profile",
    method: "get",
  });
}

// 修改用户个人信息
export function updateUserProfile(data) {
  return request({
    url: "/admin/base/system/user/profile/update",
    method: "post",
    data: data,
  });
}

// 校验验证码
export function checkPhoneCode(data) {
  return request({
    url: "/admin/base/system/user/profile/checkPhoneCode",
    method: "get",
    params: data,
  });
}

// 当前用户密码修改
export function updateUserPwd(oldPassword, newPassword) {
  const data = {
    oldPassword,
    newPassword,
  };
  return request({
    url: "/admin/base/system/user/profile/updatePwd",
    method: "post",
    data: data,
  });
}

// 首次登录修改密码
export function updatePwdFirst(data) {
  return request({
    url: "/admin/base/system/user/profile/updatePwdFirst",
    method: "post",
    data,
  });
}

export function getRoleByTenant(tenantId) {
  return request({
    url: "/admin/system/role/getRoleByTenant/" + praseStrEmpty(tenantId),
    method: "get",
  });
}

export function getRoleByTenantTT(tenantId, industryCode, appVerCode) {
  return request({
    url: `/ttadmin/ttview/system/role/getRoleByTenant/${tenantId}/${industryCode}/${appVerCode}`,
    method: "get",
  });
}

//根据租户id模糊查询当前用户可以看到哪些租户管理员
export function getTenantByTenantName2(tenantName) {
  return request({
    url: "/admin/system/tenant/selectTenantUserByKeyword?keyword=" + tenantName,
    method: "get",
  });
}

// 查询配置
export function getPreference(data) {
  return request({
    url: "/admin/userPreference/getPreference",
    method: "post",
    data,
  });
}

// 修改配置
export function modify(data) {
  return request({
    url: "/admin/userPreference/modify",
    method: "post",
    data,
  });
}
//获取用户下面待办事项
export function getUserTaskWait(data) {
  return request({
    url: "/event/alarm/flowdeploy/getFlowsByUser",
    method: "post",
    data,
  });
}

// 判断手机号是否与账号匹配
export function getStateByUserNameAndPhone(data) {
  let url = "/auth/getStateByUserNameAndPhone";
  if (data?.toNode) {
    url += `?toNode=${data.toNode}`;
  }
  const userName = data.username;
  data.decryptFlag = "1"; //是否需要解密标识（1需要，0不需要）
  data.username = aes.encrypt(userName);
  return request({
    url: url,
    method: "post",
    data,
  });
}

// 获取验证码
export function sendVerifyCode(data) {
  let url = "/auth/sendVerifyCode";
  if (data?.toNode) {
    url += `?toNode=${data.toNode}`;
  }
  return request({
    url: url,
    method: "post",
    data,
  });
}

// 校验验证码
export function checkVerifyCode(params) {
  return request({
    url: "/auth/checkVerifyCode",
    method: "get",
    params,
  });
}

// 保存重置密码
export function updateByForgerPassword(data) {
  let url = "/auth/updateByForgerPassword";
  if (data?.toNode) {
    url += `?toNode=${data.toNode}`;
  }
  const userName = data.username;
  data.decryptFlag = "1"; //是否需要解密标识（1需要，0不需要）
  data.username = aes.encrypt(userName);
  return request({
    url: url,
    method: "post",
    data,
  });
}

// 查询是否过期
export function isNeedUpdate(data) {
  return request({
    url: "/admin/system/user/isNeedUpdate",
    method: "post",
    data,
  });
}

/**
 * 获取二维码
 */
export function getQrCode(params) {
  return request({
    url: "/auth/getQrCode",
    method: "get",
    params,
  });
}

/**
 * 查询二维码状态
 */
export function checkQrCode(data) {
  return request({
    url: "/auth/checkQrCode",
    method: "post",
    data,
  });
}

/**
 * 二维码登录
 */
export function doQrLogin(data) {
  return request({
    url: "/auth/doQrLogin",
    method: "post",
    data,
  });
}

/**
 * 验证节点编号是否存在
 * @param data
 * @returns {*}
 */
export function verifyNode(data) {
  return request({
    url: "/cascade/platformData/exist",
    method: "post",
    data,
  });
}

/**
 * 校验密码是否与旧密码重复
 * @param data
 * @returns {*}
 */
export function checkOnlyPasswordRepeat(data) {
  let url = "/auth/checkOnlyPasswordRepeat";
  if (data?.toNode) {
    url += `?toNode=${data.toNode}`;
  }
  const userName = data.username;
  data.decryptFlag = "1"; //是否需要解密标识（1需要，0不需要）
  data.username = aes.encrypt(userName);
  return request({
    url: url,
    method: "post",
    data,
  });
}

/**
 * 用户登录账号IP白名单列表(客户）
 * @param data
 * @returns {*}
 */
export function pageWhiteIps(data) {
  return request({
    url: "/admin/system/user/pageWhiteIps",
    method: "post",
    data,
  });
}

/**
 * 用户登录IP白名单弹窗设置用户是否校验白名单(客户）
 * @param data
 * @returns {*}
 */
export function checkLoginIp(data) {
  return request({
    url: "/admin/system/user/checkLoginIp",
    method: "post",
    data,
  });
}

/**
 * 用户登录IP白名单弹窗设置用户保存(客户）
 * @param data
 * @returns {*}
 */
export function opreatorWhiteIps(data) {
  return request({
    url: "/admin/system/user/opreatorWhiteIps",
    method: "post",
    data,
  });
}
// 刷新字典缓存
export function getBannerColor() {
  return request({
    url: "/manage/industry/config/queryIndustryVerConf",
    method: "post",
  });
}
